/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "os_obj.h"
#define _SYSTASK_MODULE_
#include "systask.h"
#undef _SYSTASK_MODULE_
#include "pincfg.h"
#include "sys_init.h"
#include "password.h"
#include "dataproc.h"
#include "bsp_dwt.h"
#include "str_ext.h"

__USED char * clist[] = 
{
    "123",
    "124",
    "125",
    "126",
    "127",
    "128",
    "129",
    0
};

    void _flash_Erase(uint32_t AddrBase);                /*擦除*/
    void _flash_Program(uint32_t Addr, uint32_t dat);    /*编程一字*/
    uint32_t _flash_Read(uint32_t Addr);                 /*读取一字*/


void stack_test(void);
/*****************************************************************************//*!
*
* @brief   storage task.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void SysTask(void const *argument)
{
    uint32_t index;

    os_obj_creat();

    sys_init();

    sys_st.tpwms.en = 0;
    
    strcpy(sys_st.str_buff, " -123.3sy-123.3s\r\n\t\t\t_st.23pu-.4lse, sys_st.u_tick");
    copy_u8("123",sys_st.password_asc,3);
    sys_st.i_air_temp = 1200;
    
    
    if(sys_st.log_w[0] == UINT32_MAX)
    {
        sys_st.log_w[0] = 0x100;
    }
    sys_st.log_w[0] &= 0xFFFFFFF0;
    
    
    while (1)
    {
		bsp_dwt_check();
		
		sc_pulse_creat(sys_st.u_tick);
        pulse_creat(&sys_st.pulse, sys_st.u_tick);
        if (sys_st.pulse.pulse_1s)
        {
            TimeRun(&sys_st.rtc_time);
        }
        sys_st.sig_time = time_to_sig(&sys_st.rtc_time);

		sys_st.api_tick.quene_dat[0]++;
		sys_st.api_tick.quene_dat[1]++;
		sys_st.api_tick.quene_dat[1]++;
		
        NOP1();
        osDelay(20);
        NOP1();
        
        average_segmentation_u16(sys_st.duty, 30, sys_st.duty_cal_list);
        sys_st.tpwms.duty_set_list = sys_st.duty_cal_list;
        sys_st.tpwms.duty_now_list = sys_st.duty_out_list;
        sys_st.tpwms.duty_len = 30;
        sys_st.tpwms.period_set = 10;

		sys_st.alsin_out = get_2n_alain_val(sys_st.alain_src);
		
		// 信号量测试
		osSemaphoreAcquire(osobj.sid_tst,0);
		osSemaphoreAcquire(osobj.sid_tst,0);
		sys_st.api_tick.sem_req = 1;
		osSemaphoreAcquire(osobj.sid_tst, INT32_MAX);
		sys_st.api_tick.sem_rcv_tick = bsp_cpu_clks_get();
		sys_st.api_tick.sem_tick_amount = 
			sys_st.api_tick.sem_rcv_tick - sys_st.api_tick.sem_send_tick;
		
		// 事件组测试
		osSignalWait(0, 0);
		osSignalWait(0, 0);
		sys_st.api_tick.event_req = 1;
		osSignalWait(0, INT32_MAX);
		sys_st.api_tick.event_rcv_tick = bsp_cpu_clks_get();
		sys_st.api_tick.event_tick_amount = 
			sys_st.api_tick.event_rcv_tick - sys_st.api_tick.event_send_tick;
		
		// 队列测试
		osMessageQueueGet(&osobj.quene_tst, &sys_st.api_tick.quene_dat[2],NULL, 1);
		osMessageQueueGet(&osobj.quene_tst, &sys_st.api_tick.quene_dat[2],NULL, 1);
		
		sys_st.api_tick.quene_req = 1;
		osMessageQueueGet(&osobj.quene_tst, &sys_st.api_tick.quene_dat[2],NULL, INT16_MAX);
		sys_st.api_tick.quene_rcv_tick = bsp_cpu_clks_get();
		sys_st.api_tick.quene_tick_amount = 
			sys_st.api_tick.quene_rcv_tick - sys_st.api_tick.quene_send_tick;
		
		
		// 互斥量获取
		/*
		sys_st.api_tick.mutex_req_tick = bsp_cpu_clks_get();
		tx_mutex_get(&os_obj.mutex_common, TX_WAIT_FOREVER);
		sys_st.api_tick.mutex_get_tick_amount = bsp_cpu_clks_get() - sys_st.api_tick.mutex_req_tick;
		
		sys_st.api_tick.mutex_req_tick = bsp_cpu_clks_get();
		tx_mutex_put(&os_obj.mutex_common);
		sys_st.api_tick.mutex_put_tick_amount = bsp_cpu_clks_get() - sys_st.api_tick.mutex_req_tick;
		*/
		
		// 互斥量等待获取
		
		if(sys_st.api_tick.b_mutex_busy)
		{
			sys_st.api_tick.mutex_req = 1;
			osMutexAcquire(osobj.mid_tst, INT32_MAX);
			
			sys_st.api_tick.mutex_get_tick = bsp_cpu_clks_get();
			sys_st.api_tick.mutex_get_tick_amount = 
				sys_st.api_tick.mutex_get_tick - sys_st.api_tick.mutex_put_tick;
			
			osMutexRelease(osobj.mid_tst);
		}
    }
}

void SnycTask(void const *argument)
{
	//
	osDelay(5);
	
	while(1)
	{
		osDelay(10);
		
		if(sys_st.api_tick.sem_req)
		{
			sys_st.api_tick.sem_req = 0;
			sys_st.api_tick.sem_send_tick = bsp_cpu_clks_get();
			osSemaphoreRelease(osobj.sid_tst);
		}
		
		if(sys_st.api_tick.event_req)
		{
			sys_st.api_tick.event_req = 0;
			sys_st.api_tick.event_send_tick = bsp_cpu_clks_get();
			osSignalSet(osobj.tid_SysTask, 1);
		}
		
		if(sys_st.api_tick.quene_req)
		{
			sys_st.api_tick.quene_req = 0;
			sys_st.api_tick.quene_send_tick = bsp_cpu_clks_get();
			osMessageQueuePut(&osobj.quene_tst, &sys_st.api_tick.quene_dat[0], 0, 0);
			osMessageQueuePut(&osobj.quene_tst, &sys_st.api_tick.quene_dat[0], 0, 0);
			osMessageQueuePut(&osobj.quene_tst, &sys_st.api_tick.quene_dat[0], 0, 0);
		}
		
		if(!sys_st.api_tick.b_mutex_busy)
		{
			osMutexAcquire(osobj.mid_tst, INT32_MAX);
			sys_st.api_tick.b_mutex_busy = 1;
		}
		
		if(sys_st.api_tick.mutex_req)
		{
			sys_st.api_tick.mutex_req = 0;
			sys_st.api_tick.mutex_put_tick = bsp_cpu_clks_get();
			osMutexRelease(osobj.mid_tst);
			sys_st.api_tick.b_mutex_busy = 0;
		}
		
	}
}

void isr_sync_send(void)
{
		if(sys_st.api_tick.sem_req)
		{
			sys_st.api_tick.sem_req = 0;
			sys_st.api_tick.sem_send_tick = bsp_cpu_clks_get();
			osSemaphoreRelease(osobj.sid_tst);
		}
		
		if(sys_st.api_tick.event_req)
		{
			sys_st.api_tick.event_req = 0;
			sys_st.api_tick.event_send_tick = bsp_cpu_clks_get();
			osSignalSet(osobj.tid_SysTask, 1);
		}
		
		if(sys_st.api_tick.quene_req)
		{
			sys_st.api_tick.quene_req = 0;
			sys_st.api_tick.quene_send_tick = bsp_cpu_clks_get();
			osMessageQueuePut(&osobj.quene_tst, &sys_st.api_tick.quene_dat[0], 0, 0);
		}
}


/*****************************************************************************//*!
*
* @brief   task switch hook.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_sw_hook(char pos)
{

}

/*****************************************************************************//*!
*
* @brief   task switch hook.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sw_pwm_out(void)
{
    sys_st.pwm_tick_cnt++;
    if(sys_st.pwm_tick_cnt >= 10)
    {
        sys_st.pwm_tick_cnt = 0;
        sim_pwm_list_handle(&sys_st.tpwms);
        if(sys_st.tpwms.out)
        {
            
        }
        else
        {
            
        }
    }
}


void stack_test(void)
{
 
}


void _flash_Erase(uint32_t AddrBase)
{
        FLASH_Unlock();
        FLASH_ErasePage(AddrBase);
        FLASH_Lock();
}

    void _flash_Program(uint32_t Addr, uint32_t dat)
    {
        FLASH_Unlock();
        FLASH_ProgramWord(Addr, dat);
        FLASH_Lock();
    }
    
    uint32_t _flash_Read(uint32_t Addr)
    {
        return inpw(Addr);
    }
